package com.opencloud.ywt.provider.controller;

import com.alibaba.fastjson.JSONObject;
import com.opencloud.common.model.PageParams;
import com.opencloud.common.model.ResultBody;
import com.opencloud.ywt.client.model.entity.BhyfProvince;
import com.opencloud.ywt.provider.service.BhyfCityService;
import com.opencloud.ywt.client.model.entity.BhyfCity;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.core.metadata.IPage;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 园务通（城市）
 * @author yeyong
 */
@Slf4j
@RestController
@Api(value = "园务通", tags = "园务通")
public class BhyfCityController {
    @Autowired
    private BhyfCityService bhyfCityService;

    /**
     * 获取城市信息
     * @param map
     * @return
     */
    @ApiOperation(value = "获取城市信息", notes = "获取城市信息")
    @GetMapping("/bhyfcity/infos")
    public ResultBody<IPage<BhyfCity>> getCityListPage(
            @RequestParam(required = false) Map map
    )
    {
        return ResultBody.ok().data(bhyfCityService.findListPage(new PageParams(map)));
    }


    /**
     * 获取公众号UnionId
     * @param openid
     * @return
     */
    @ApiOperation(value = "获取公众号UnionId", notes = "获取公众号UnionId")
    @GetMapping("/bhyfcity/testgetid")
    public ResultBody<String> TestGetUnionId(String openid)
    {
        Map<String,String> params = new HashMap<>();
        params.put("appid", "wx8667c387ebd81529");
        params.put("secret", "b03cdc3d5be20d2e673c6b40ac03d8b6");
        params.put("grant_type", "client_credential");
        //31_DMr27VB51nVA3c7IOpmAW84cvGAhhuwdl1LfQFwz4kK5wFowH1hbCfjKoYGfU49cT37HQjspcXH2hbHnObSair5Yhsm9DBh1RI4Z10j4EKfjqNb7DS-UgAnLwtLmKNrcO8dGCgLRJL8_wMuoWBKfAIALQZ
        String result = HttpGetUtil.httpRequestToString("https://api.weixin.qq.com/cgi-bin/token", params);
        JSONObject jsonObject = JSONObject.parseObject(result);
        log.error("result:"+result);
        if(jsonObject!=null) {
            String access_token = jsonObject.get("access_token").toString();
            if (!access_token.isEmpty()) {
                String unionId = GetUnionId(access_token, openid);
                return ResultBody.ok().data(unionId);
            } else {
                return ResultBody.failed().data("");
            }
        }else {
            return ResultBody.failed().data("");
        }
    }

    public String GetUnionId(String access_token,String openid)
    {
        Map<String,String> params = new HashMap<>(); params.put("access_token",access_token);
        params.put("lang","zh_CN");
        params.put("openid",openid);
        String result = HttpGetUtil.httpRequestToString("https://api.weixin.qq.com/cgi-bin/user/info", params);
        log.error("result:"+result);
        //JSONObject jsonObject = JSONObject.parseObject(result);
        //String subscribe = jsonObject.get("unionid").toString();
        return result;
    }

    @ApiOperation(value = "获取省份", notes = "获取省份")
    @GetMapping("/bhyfcity/province")
    public ResultBody<List<BhyfProvince>> getProvinceList(
    )
    {
        return ResultBody.ok().data(bhyfCityService.getProvince());
    }
}
